package com.srzk.youthleagueback.mapper;

import com.srzk.youthleagueback.pojo.Lecture;
import com.srzk.youthleagueback.pojo.Project;
import com.srzk.youthleagueback.pojo.Sport;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class ProjectProvider extends SQL {
    private static final String TABLE_NAME = "project";

    public String updateProject(@Param("project") Project project) {
        return new SQL() {{
            UPDATE(TABLE_NAME);
            if (!StringUtils.isEmpty(project.getUser())) {
                SET("user = #{user}");
            }
            if (!StringUtils.isEmpty(project.getYear())) {
                SET("year= #{year}");
            }
            if (!StringUtils.isEmpty(project.getId().toString())) {
                SET("id= #{id}");
            }
            if (!ObjectUtils.isEmpty(project.getTopic())) {
                SET("topic= #{topic}");
                SET("leader= #{leader}");
                SET("tutor= #{tutor}");
                SET("members= #{members}");
                SET("content= #{content}");
                SET("awards= #{awards}");
                SET("note= #{note}");
            }
            WHERE("id = #{id}");
            WHERE("user = #{user}");
            WHERE("year= #{year}");
        }}.toString();
    }

    public String insertProject(@Param("project") Project project){
        return new SQL() {{
            INSERT_INTO(TABLE_NAME);
           try {
               Map<String,Object> data = ObjectUtil.reflect(project);
               data.forEach((key,value)->{
                   if(value!=null){
                       VALUES(key, "\'"+value.toString()+"\'");
                   }
               });
           }catch (Exception e){
               e.printStackTrace();
           }
        }}.toString();
    }
}
